package com.ebelter.btcomlib.models.bluetooth.products.bpm;


import android.content.Context;
import android.text.TextUtils;

import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.models.bluetooth.base.BaseManager;

import com.ebelter.btcomlib.models.bluetooth.bean.ProductStyle;

import com.ebelter.btcomlib.models.bluetooth.bean.SendMessage;
import com.ebelter.btcomlib.models.bluetooth.interfaces.IDataReadWriteCallback;
import com.ebelter.btcomlib.models.bluetooth.products.bpm.interfaces.BPMMeasureCallback;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;

/**
 * 血压计
 */
public class BpmManager extends BaseManager {

    private static final String TAG = "BpmManager";


    private BPMBytesAnalysis mBPMBytesAnalysis;
    private BPMBytesMake mBPMBytesMake;

    private String connectName;

    public BpmManager(Context mContext) {
        super(mContext, ProductStyle.BPM, BPMConstant.BPM_SERVICE_UUID,
                new String[]{BPMConstant.BPM_WRITE_UUID}, new String[]{BPMConstant.BPM_READ_UUID},
                true, 300);

        mBPMBytesAnalysis = new BPMBytesAnalysis();
        mBPMBytesMake = new BPMBytesMake();

        mServiceGatt.setIDataReadWriteCallback(new IDataReadWriteCallback() {
            @Override
            public void onCharacteristicChanged(byte[] data) {
                LogUtils.i(TAG, "BpmManager--IDataReadWriteCallback-onCharacteristicChanged--读到-" + BytesUtils.bytes2HexStr(data));
                if (mBPMBytesAnalysis != null) mBPMBytesAnalysis.resultAnalysis(data);
            }

            @Override
            public void writeData(byte[] data) {
                // LogUtils.i(TAG,"ScaleBlue--IDataReadWriteCallback-writeData--写入-"+ Arrays.toString(data));
            }
        });
    }

    public void setConnectName(String name) {
        if (mBPMBytesAnalysis != null) {
            mBPMBytesAnalysis.setConnectName(name);
        }
        connectName = name;
    }


//    public boolean isBondDevice() {
//        String add = getBondDeviceAddress();
//        return !TextUtils.isEmpty(add);
//    }

    public String getBondDeviceAddress() {
        return SpUtil.readString(BPMConstant.BPM_BOND_DEVICE_ADDRESS, null);
    }

    public void setBondDeviceAddress(String address) {
        SpUtil.writeString(BPMConstant.BPM_BOND_DEVICE_ADDRESS, address);
    }


    public void setBpmMeasureCallback(BPMMeasureCallback bpmMeasureCallback) {
        mBPMBytesAnalysis.setBpmMeasureCallback(bpmMeasureCallback);
    }


    /**
     * @param ss 收缩压
     * @param sz 舒张压
     */
    public static String getMesureResultDesc(int ss, int sz) {
        LogUtils.i(TAG, "ss=" + ss + "---sz=" + sz);


        if ((90 <= ss && ss < 120) && (60 <= sz && sz < 80)) {
            return StringUtils.getResStr(R.string.Normal_blood_pressure);
        } else if (ss >= 140 && sz < 90) {
            return StringUtils.getResStr(R.string.Isolated_systolic_hypertension);
        } else {

            if (ss < 90) {
                return StringUtils.getResStr(R.string.Low_blood_pressure);
            } else if (120 <= ss && ss <= 139) {
                return StringUtils.getResStr(R.string.High_normal_blood_pressure);
            } else if (140 <= ss && ss < 159) {
                return StringUtils.getResStr(R.string.Grade_1_hypertension);

            } else if (160 <= ss && ss < 179) {
                return StringUtils.getResStr(R.string.Grade_2_hypertension);

            } else if (180 <= ss) {
                return StringUtils.getResStr(R.string.Grade_3_hypertension);

            }

            if (sz < 60) {
                return StringUtils.getResStr(R.string.Low_blood_pressure);
            } else if (80 <= sz && sz <= 89) {
                return StringUtils.getResStr(R.string.High_normal_blood_pressure);
            } else if (90 <= sz && sz < 99) {
                return StringUtils.getResStr(R.string.Grade_1_hypertension);
            } else if (100 <= sz && sz < 109) {
                return StringUtils.getResStr(R.string.Grade_2_hypertension);
            } else if (110 <= sz) {
                return StringUtils.getResStr(R.string.Grade_3_hypertension);
            }

        }
        return "";
    }


    /**
     * @param ss 收缩压
     * @param sz 舒张压
     * @return 0低血压 1正常血压 2正常血压高值 3单纯收缩性高血压 4一级高血压 5二级高血压 6三级高血压
     */
    public static int getMesureResultLevel(int ss, int sz) {
        LogUtils.i(TAG, "ss=" + ss + "---sz=" + sz);

        if ((90 <= ss && ss < 120) && (60 <= sz && sz < 80)) {
            return 1;
        } else if (ss >= 140 && sz < 90) {
            return 3;
        } else {
            if (ss < 90) {
                return 0;
            } else if (120 <= ss && ss <= 139) {
                return 2;
            } else if (140 <= ss && ss < 159) {
                return 4;
            } else if (160 <= ss && ss < 179) {
                return 5;
            } else if (180 <= ss) {
                return 6;
            }

            if (sz < 60) {
                return 0;
            } else if (80 <= sz && sz <= 89) {
                return 2;
            } else if (90 <= sz && sz < 99) {
                return 4;
            } else if (100 <= sz && sz < 109) {
                return 5;
            } else if (110 <= sz) {
                return 6;
            }

        }
        return 1;
    }


    /**
     * 测量错误
     * errorType=1 传感器震荡异常,设备LCD显示ERR1
     * errorType=2 检测不到足够的心跳或算不出血压，设备LCD显示ERR2
     * errorType=3 测量结果异常，设备LCD显示ERR3
     * errorType=4 测量结果异常或压力超过上限，设备LCD显示ERR H
     * errorType=5 袖带过松或漏气(10秒内加压不到30mmHg)，设备LCD显示ERR U
     * errorType=6 电池电量过低，提示用户更换电池, LCD显示
     */
    public static String getErrorInfoDesc(int errorCode) {
        String result;
        if (errorCode == 1) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_1);
        } else if (errorCode == 2) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_2);
        } else if (errorCode == 3) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_3);
        } else if (errorCode == 4) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_4);
        } else if (errorCode == 5) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_5);
        } else if (errorCode == 6) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_6);
        } else {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_7);
        }
        return result;
    }

    public static String getErrorInfoDesc2(int errorCode) {
        String result;
        if (errorCode == 1) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_2);
        } else if (errorCode == 2) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_1);
        } else if (errorCode == 3) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_5);
        } else if (errorCode == 5) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_3);
        } else if (errorCode == 7) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_7);
        } else if (errorCode == 0x0b) {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_6);
        } else {
            result = StringUtils.getResStr(R.string.bpm_mesure_error_tip_7);
        }

        return result;
    }


    private static final String ReceiveSuccessAckDesc = "数据接收成功，停止发送数据";
    private static final byte[] ReceiveSuccessAckData1 = new byte[]{(byte) 0xff, (byte) 0xfd, 0x02, 0x05};
    private static final byte[] ReceiveSuccessAckData2 = new byte[]{0x02, 0x40, (byte) 0xdd, 0x04, (byte) 0xff, (byte) 0xfd, 0x02, 0x05, (byte) 0x9c};

    public boolean sendReceiveSuccessACK() {
        SendMessage message = new SendMessage();
        message.desc = ReceiveSuccessAckDesc;
        if (TextUtils.equals(BPMConstant.BPM_NAME,connectName)){
            message.datas=ReceiveSuccessAckData1;
        }else {
            message.datas=ReceiveSuccessAckData2;
        }
        return mServiceGatt != null && mServiceGatt.addSendMsg(message);
    }


}
